<template>
	<div class="app-container">
		<div class="filter-container">
			<div class="fr">
      <el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="el-icon-edit">添加</el-button>
      </div>
    </div>
		<el-table class="tqd_table" :key='tableKey' :data="list" v-loading="listLoading" border fit highlight-current-row
      style="width: 100%;">
      <!-- <el-table-column
      align="center"
      type="selection"
      width="55">
    	</el-table-column> -->
			<el-table-column width="60" label="序号" show-overflow-tooltip>
        <template slot-scope="scope">{{scope.row.id}}
        </template>
      </el-table-column>
      <el-table-column label="账号" show-overflow-tooltip>
        <template slot-scope="scope">{{scope.row.account}}
        </template>
      </el-table-column>
      <el-table-column label="别名" show-overflow-tooltip>
        <template slot-scope="scope">{{scope.row.nickName}}
        </template>
      </el-table-column>
			<el-table-column width="150" label="密码" show-overflow-tooltip>
        <template slot-scope="scope">{{scope.row.password}}
        </template>
      </el-table-column>
      <el-table-column width="150" label="是否登录" show-overflow-tooltip>
        <template slot-scope="scope">
          <span v-if="scope.row.isLogin==0">未登录</span>
          <span v-if="scope.row.isLogin==1">已登录</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="300" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="restLoginState(scope.row.id)">重置登录</el-button>
          <el-button size="mini" type="danger" @click="handleDelete([scope.row.id])">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page" :page-sizes="[15,30, 50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div> -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="550px">
      <el-form :rules="rules" ref="dataForm" :model="temp" label-position="right" label-width="70px" style='width: 100%; '>
        <el-form-item prop="account" label="账号">
          <el-input type="input" v-model="temp.account" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item prop="nickName" label="名称">
          <el-input type="input" v-model="temp.refereeName" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">确定</el-button>
        <el-button v-else type="primary" @click="updateData">确定</el-button>
      </div>
    </el-dialog>
	</div>

</template>
<script>
import { getRefereeList, saveReferee, deleteReferee, restLoginState } from '@/api/referee'
import waves from '@/directive/waves' // 水波纹指令
import { showMsg } from '@/utils'
export default {
  name: 'complexTable',
  directives: {
    waves
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 15,
        importance: undefined,
        title: undefined,
        type: undefined,
        sort: '+id'
      },
      temp: {
        matchId: null,
        refereeName: null,
        account: null
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '修改',
        create: '添加'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        account: [{ required: true, message: '必填', trigger: 'blur' }],
        refereeName: [{ required: true, message: '必填', trigger: 'blur' }]
      },
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  mounted() {

  },
  methods: {
    getList() {
      this.listLoading = true
      getRefereeList({ matchId: this.$route.query.id }).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作成功',
        type: 'success'
      })
      row.status = status
    },
    restLoginState(id) {
      restLoginState(id).then(res => {
        if (res.data.code === 0) {
          showMsg(res.data.message, 1)
          this.getList()
        } else {
          showMsg(res.data.message, 2)
        }
      })
    },
    resetTemp() {
      this.temp = {
        matchId: null,
        refereeName: null,
        account: null
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.temp.matchId = parseInt(this.$route.query.id)
          saveReferee(this.temp).then(res => {
            if (res.data.code === 0) {
              showMsg(res.data.message, 1)
              this.dialogFormVisible = false
              this.getList()
            } else {
              showMsg(res.data.message, 2)
            }
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.temp.timestamp = new Date(this.temp.timestamp)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          tempData.timestamp = +new Date(tempData.timestamp) // change Thu Nov 30 2017 16:41:05 GMT+0800 (CST) to 1512031311464
          saveReferee(tempData).then(() => {
            for (const v of this.list) {
              if (v.id === this.temp.id) {
                const index = this.list.indexOf(v)
                this.list.splice(index, 1, this.temp)
                break
              }
            }
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleDelete(id) {
      this.$confirm('您确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteReferee(id).then(res => {
          if (res.data.code === 0) {
            showMsg(res.data.message, 1)
            this.getList()
          } else {
            showMsg(res.data.message, 2)
          }
        })
      }).catch(() => {
        console.log('=')
      })
    }
  }
}
</script>